package cn.tedu.springboot.security.controller;

import cn.tedu.springboot.security.pojo.dto.AdminLoginDTO;
import cn.tedu.springboot.security.pojo.vo.AdminListItemVO;
import cn.tedu.springboot.security.pojo.vo.AdminLoginInfoVO;
import cn.tedu.springboot.security.service.IAdminService;
import cn.tedu.springboot.security.sety.LoginPrincipal;
import cn.tedu.springboot.security.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author BJ
 * @date 2022/10/22 15:44
 */
@Slf4j
@Api(tags = "01. 管理员模块")
@RestController
@RequestMapping("/admins")
public class AdminController {
    @Autowired
    IAdminService adminService;

    @ApiOperation("管理员登录")
    @ApiOperationSupport(order = 100)
    @PostMapping("/login")
    public JsonResult<AdminLoginInfoVO> login(AdminLoginDTO adminLoginDTO){
        log.debug("开始处理 [管理员登录] 的请求,参数:{}",adminLoginDTO);
        String jwt = adminService.login(adminLoginDTO);
        return JsonResult.ok(jwt);
    }

    @ApiOperation("管理员列表")
    @ApiOperationSupport(order = 600)
    @PreAuthorize("hasAuthority('/ams/admin/read')") //访问所需权限
    @GetMapping("")
    public JsonResult<List<AdminListItemVO>> list(
            @ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal){
        log.debug("开始处理【查询管理员】的请求...");
        log.debug("当前登录的当事人：{}", loginPrincipal); // 可以控制台观察数据
        List<AdminListItemVO> list = adminService.list();
        return JsonResult.ok(list);
    }

}
